package com.xiaoyu.client.ui.activity.main.mine.sign_and_help;


import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xiaoyu.client.R;
import com.xiaoyu.client.model.mine.IntegralListBean;
import com.xiaoyu.client.model.mine.ParticularsBean;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.adapter.AllPowerfulAdapter;
import com.xiaoyu.commonlib.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author jx
 * @date 创建时间 2018/10/17
 * @Description 积分记录
 */
@SuppressLint("ValidFragment")
public class IntegralRecordFragment extends Fragment {

    private int type;
    private View mView;
    @BindView(R.id.common_recycler_view)
    RecyclerView mRecyclerView;
    private AllPowerfulAdapter mMyAdapter;
    private List<IntegralListBean.DataBean.JifenlistBean> mList = new ArrayList<>();
    @BindView(R.id.smart_refresh_layout)
    SmartRefreshLayout mRefreshLayout;

    private int page = 1;//页数
    private int count = 0;//数据总条数

    public IntegralRecordFragment(int type) {
        this.type = type;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(R.layout.common_recycler_view, container, false);
            ButterKnife.bind(this, mView);
            initView();
        }
        return mView;
    }

    private void initView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        setMyAdapter(mList);


        //上拉下拉刷新设置
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @NonNull
            @Override
            public RefreshHeader createRefreshHeader(@NonNull Context context, @NonNull RefreshLayout layout) {
                return new ClassicsHeader(context);
            }
        });
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @NonNull
            @Override
            public RefreshFooter createRefreshFooter(@NonNull Context context, @NonNull RefreshLayout layout) {
                return new ClassicsFooter(context).setDrawableSize(20);
            }
        });
        mRefreshLayout.setOnRefreshListener(onRefreshListener);
        mRefreshLayout.setOnLoadMoreListener(onLoadMoreListener);
        mRefreshLayout.setNoMoreData(false);

        if (type == 0) {

        } else {
            getIntegralList();//积分列表
        }

    }

    private void setMyAdapter(List<IntegralListBean.DataBean.JifenlistBean> myAdapterList) {
        mMyAdapter = new AllPowerfulAdapter<IntegralListBean.DataBean.JifenlistBean>(R.layout.integral_record_item, myAdapterList) {
            @Override
            protected void convert(BaseViewHolder baseViewHolder, IntegralListBean.DataBean.JifenlistBean jifenlistBean) {
                super.convert(baseViewHolder, jifenlistBean);
                baseViewHolder.setText(R.id.activity_address_name, jifenlistBean.getJifentype());
                baseViewHolder.setText(R.id.activity_address_street, jifenlistBean.getJifentime());
                baseViewHolder.setText(R.id.activity_address_mobile, jifenlistBean.getJifen());
            }
        };
        mRecyclerView.setAdapter(mMyAdapter);
    }


    //刷新监听
    private OnRefreshListener onRefreshListener = new OnRefreshListener() {
        @Override
        public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            page = 1;
            mMyAdapter.notifyDataSetChanged();
            getIntegralList();
            mRefreshLayout.finishRefresh();
        }
    };

    //加载更多
    private OnLoadMoreListener onLoadMoreListener = new OnLoadMoreListener() {
        @Override
        public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
            if (page < count) {
                page = page + 1;
                getIntegralList();
                mRefreshLayout.finishLoadMore();
                mRefreshLayout.setNoMoreData(false);
            } else {
                mRefreshLayout.finishLoadMore();
                mRefreshLayout.setNoMoreData(true);
                mRefreshLayout.finishLoadMoreWithNoMoreData();
                ToastUtil.showBelowToast(getActivity(), "已经没有更多数据！！！");
            }
        }
    };




    //获取积分列表
    private void getIntegralList() {
        NetworkManager.integralList(page, new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                succeedResult(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
            }
        });
    }

    //解析积分列表数据
    private void succeedResult(String result) {
        if (page == 1){
            mList.clear();
            mMyAdapter.notifyDataSetChanged();
        }
        IntegralListBean listBean = new Gson().fromJson(result, IntegralListBean.class);
        count = listBean.getData().getCount();//总个数
        mList.addAll(listBean.getData().getJifenlist());
        mMyAdapter.notifyDataSetChanged();
    }



}
